package com.sky.wsp.job.util;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.concurrent.TimeUnit;

/**
 * @author wangshaopeng@talkweb.com.cn
 * @desccription 在此输入描述
 * @date 2023/8/14
 */
public class ChromeDriverUtil {
    private static WebDriver driver = null;
    // TODO 请改成你本地的驱动文件存放地址
    // 驱动下载地址：https://github.com/mozilla/geckodriver/releases
//    private static String driverLocation = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe";
    // chromedriver下载地址： https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json
    private static String driverLocation = "C:\\Program Files\\Google\\Chrome\\Application\\driver\\chromedriver-win64-125\\chromedriver.exe";

    private static void setUp() {
        System.setProperty("webdriver.chrome.driver", driverLocation);
        ChromeOptions chromeOptions = new ChromeOptions();
        // selenium 端口方式调用浏览器
        // 使用cmd运行chrome，开启debug端口（必须指定user-data-dir参数，否则启动不成功）
        // C:\Program Files\Google\Chrome\Application>chrome.exe --remote-debugging-port 9222 --user-data-dir=d:\chrome
//        chromeOptions.addArguments("--remote-debugging-port=9222");
        chromeOptions.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
//        chromeOptions.setExperimentalOption("debuggerAddress", "localhost:9222");

        driver = new ChromeDriver(chromeOptions);

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    public static WebDriver getDriver() {
        if (null == driver) {
            setUp();
        }
        return driver;
    }

    public static void driverQuit() {
        if (null == driver) {
            return;
        }
        driver.quit();
        driver = null;
    }


}
